Imports System.Windows.Forms

Public Class dlgCoupon
    Private ID As String
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim sql As String
        If db.getvalue("select * from Coupon where CouponID = '" & cbCouponID.Text.Replace("'", "''") & "'").Tables(0).Rows.Count > 0 Then
            MsgBox("Duplicate!!!")
            Exit Sub
        End If
        If Not checkCouponTime(DTPBeginTime.Value, DTPEndTime.Value, cbCouponID.Text) Then
            MsgBox("The time is not available.")
            Exit Sub
        End If
        sql = "insert into Coupon(CouponID,cname,cScale,cCreatedby,CBtime,CEtime)" _
        & " values('" & cbCouponID.Text.Replace("'", "''") _
        & "','" & txtCouponName.Text.Replace("'", "''") _
        & "'," & nupCouponValue.Value _
        & ",'" & LoginForm.userID _
        & "', '" & DTPBeginTime.Value _
        & "', '" & DTPEndTime.Value & "')"
        Try
            db.update(sql)
            MsgBox("the Coupon ID " & cbCouponID.Text & " has been created.")
            cbCouponID.DataSource = db.getvalue("select * from Coupon").Tables(0)
            cbCouponID.DisplayMember = "CouponID"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Dim sql As String
        'If db.getvalue("select * from Coupon where CouponID = '" & cbCouponID.Text.Replace("'", "''") & "'").Tables(0).Rows.Count <= 0 Then
        '    MsgBox("The CouponID " & cbCouponID.Text & " is not found!!!")
        '    Exit Sub
        'End If
        If Not checkCouponTime(DTPBeginTime.Value, DTPEndTime.Value, cbCouponID.Text) Then
            MsgBox("The time is not available.")
            Exit Sub
        End If
        sql = "update Coupon set " _
        & " cname = '" & txtCouponName.Text.Replace("'", "''") _
        & "', cScale = " & nupCouponValue.Value _
        & ", cCreatedby = '" & LoginForm.userID _
        & "', couponID='" & cbCouponID.Text.Replace("'", "''") _
        & "', CBtime = '" & DTPBeginTime.Value _
        & "', CEtime = '" & DTPEndTime.Value _
        & "' where CouponID ='" & ID & "'"
        Try
            db.update(sql)
            db.update("update transactions set coupon='" & cbCouponID.Text.Replace("'", "''") & "' where Coupon ='" & ID & "'")
            MsgBox("the Coupon ID " & cbCouponID.Text & " has been updated.")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub

    Private Sub cbCouponID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbCouponID.SelectedIndexChanged
        Dim ds As DataSet
        ds = db.getvalue("select * from Coupon where Couponid='" & cbCouponID.Text.Replace("'", "''") & "'")
        If ds.Tables(0).Rows.Count > 0 Then
            ID = ds.Tables(0).Rows(0).Item("couponID").ToString
            txtCouponName.Text = ds.Tables(0).Rows(0).Item("cname").ToString
            nupCouponValue.Value = ds.Tables(0).Rows(0).Item("cscale")
            DTPBeginTime.Value = IIf(IsDBNull(ds.Tables(0).Rows(0).Item("cbtime")), New Date(2000, 1, 1, 0, 0, 0), ds.Tables(0).Rows(0).Item("cbtime"))
            DTPEndTime.Value = IIf(IsDBNull(ds.Tables(0).Rows(0).Item("cetime")), New Date(2000, 1, 1, 0, 0, 0), ds.Tables(0).Rows(0).Item("cetime"))
        End If
    End Sub

    Private Sub dlgCoupon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbCouponID.DataSource = db.getvalue("select * from Coupon").Tables(0)
        cbCouponID.DisplayMember = "CouponID"
    End Sub
End Class
